package net.kts.placebook.gallery;

import java.util.ArrayList;

import net.kts.main.R;
import net.kts.placebook.dao.PlacebookDao;
import net.kts.placebook.gallery.common.ImageAdapter;
import net.kts.transrate.TranslateService;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;


public class GalleryList extends Activity {
	private PlacebookDao dao;
	private ProgressDialog mProgress;
	public static final String tagName = "fs333";

	ArrayList<Photo> photoList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 레이아웃 레퍼런스
		setContentView(R.layout.pb_image_list);
		
        mProgress		= new ProgressDialog(this);
        dao 			= new PlacebookDao(this);
		photoList = new ArrayList<Photo>();

		// 이전 액티비티로부터 데이터 추출
		// 1. venum ID 추출
		Intent intent = getIntent();
		String venueId = intent.getStringExtra("venueId");
		Log.i(tagName, "VenueID : " + venueId);
		
		// 2. 유효성 검사
		if(venueId.equals("")){
			Log.i(tagName, "PhotoList 장소 아이디가 없음");
		}
		
		// 사진 갤러리 실행
		loadPhotos(venueId);

	}
		
    private void loadPhotos(final String venueId) {
		String wait = TranslateService.translateRepository("잠시만 기다려주세요.");
		if(wait == null){
			mProgress.setMessage("Please wait a moment.");
		}else if(wait.trim().equals("")){
			mProgress.setMessage("Please wait a moment.");
		}else{
			mProgress.setMessage(wait);
		}
    	mProgress.show();
    	
    	new Thread() {
    		@Override
    		public void run() {
    			int what = 0;
    			
    			try {
    				photoList = dao.getPhotos(venueId);
    			} catch (Exception e) {
    				Log.i(tagName, "loadPhotos Exception");
    				what = 1;
    				e.printStackTrace();
    			}
    			
    			mHandler.sendMessage(mHandler.obtainMessage(what));
    		}
    	}.start();
    }

    private Handler mHandler = new Handler() {
    	@Override
    	public void handleMessage(Message msg) {
    		mProgress.dismiss();
    		
    		Log.i(tagName, msg.toString());
    		
    		if (msg.what == 0) {
    			if (photoList.size() == 0) {
    				Toast.makeText(GalleryList.this, "No Picture available", Toast.LENGTH_SHORT).show();
    				return;
    			}
    			printGallery();
    			
    		} else {
    			Toast.makeText(GalleryList.this, "Failed to load Picture", Toast.LENGTH_SHORT).show();
    		}
    	}
    };
    
    private String[] urlArray;
    private String[] thumbUrlArray;
    
	public void printGallery(){
		
		urlArray = new String[photoList.size()];
		thumbUrlArray = new String[photoList.size()];

		for(int i = 0; i < photoList.size(); i++){
			Photo fsqPhoto = new Photo();
			fsqPhoto = photoList.get(i);
			urlArray[i] = fsqPhoto.getUrl();
			thumbUrlArray[i] = fsqPhoto.getThumbnailUrl();
			Log.i(tagName, ":: 썸네일 이미지 유알엘 저장 :: " + fsqPhoto.getThumbnailUrl());
		}

		GridView gv = (GridView)findViewById(R.id.ImgGridView);

		final ImageAdapter ia = new ImageAdapter(thumbUrlArray);

		gv.setAdapter(ia);
		gv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
				Intent intent = new Intent(GalleryList.this, GalleryDetail.class);
				intent.putExtra("position", position);
				intent.putExtra("urlArray", urlArray);
				startActivity(intent);
			}
		});

	}

}